package com.example.hcmiu.englishforkid;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Locale;

import SQLite2.DatabaseHandler;
import android.os.Bundle;
import android.app.Activity;
import android.speech.tts.TextToSpeech;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;

public class AlphabetActivity extends Activity {

	//acc from xxxx
	TextToSpeech ttobj;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ttobj = new TextToSpeech(getApplicationContext(),
				new TextToSpeech.OnInitListener() {
					@Override
					public void onInit(int status) {
						if (status != TextToSpeech.ERROR) {
							ttobj.setLanguage(Locale.UK);
						}
					}
				});
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		String pageId = getIntent().getStringExtra("page");
 
		if (pageId != null) {

			if (pageId.equals("res/layout/activity_alphabet.xml")) {
				setContentView(R.layout.activity_alphabet);
			} else if (pageId.equals("res/layout/activity_animal.xml")) {
				setContentView(R.layout.activity_animal);
			} else if (pageId.equals("res/layout/activity_christmas.xml")) {
				setContentView(R.layout.activity_christmas);
			} else if (pageId.equals("res/layout/activity_color.xml")) {
				setContentView(R.layout.activity_color);
			} else if (pageId.equals("res/layout/activity_country.xml")) {
				setContentView(R.layout.activity_country);
			} else if (pageId.equals("res/layout/activity_drink.xml")) {
				setContentView(R.layout.activity_drink);
			} else if (pageId.equals("res/layout/activity_flowers.xml")) {
				setContentView(R.layout.activity_flowers);
			} else if (pageId.equals("res/layout/activity_food.xml")) {
				setContentView(R.layout.activity_food);
			} else if (pageId.equals("res/layout/activity_fruit.xml")) {
				setContentView(R.layout.activity_fruit);
			} else if (pageId.equals("res/layout/activity_geometry.xml")) {
				setContentView(R.layout.activity_geometry);
			} else if (pageId.equals("res/layout/activity_house.xml")) {
				setContentView(R.layout.activity_house);
			} else if (pageId.equals("res/layout/activity_number.xml")) {
				setContentView(R.layout.activity_number);
			} else if (pageId.equals("res/layout/activity_school.xml")) {
				setContentView(R.layout.activity_school);
			} else if (pageId.equals("res/layout/activity_vehicle.xml")) {
				setContentView(R.layout.activity_vehicle);
			}
		}
		MainActivity.CurrentActivity = this;
		markHistory();

	}

	private void markHistory() {
		if (times == null) {
			times = new Hashtable<String, Integer>();

			DatabaseHandler db = new DatabaseHandler(getApplicationContext());

			List<String> lables = db.getAllLabels();
			for (String toSpeak : lables) {
				System.out.println(toSpeak);
				Integer timeSpeak = times.get(toSpeak);
				if (timeSpeak == null) {
					timeSpeak = 0;
					times.put(toSpeak, timeSpeak);
				}
				timeSpeak++;
				times.put(toSpeak, timeSpeak);
			}
		}

		Enumeration<String> keys = times.keys();
		while (keys.hasMoreElements()) {
			try {

				String key = keys.nextElement();
				int value = times.get(key);

				ArrayList<View> views = getViewsByTag((ViewGroup) (getWindow()
						.getDecorView().getRootView()), key);
				for (int i = 0; i < views.size(); i++) {

					View v = views.get(i);

					Button b = (Button) v;

					b.setText(value + "");

				}
			} catch (Exception e) {
				System.out.println("=================================");
				e.printStackTrace();
			}

		}
	}

	public void TalkClick(View v) {
		Button b = (Button) v;

		String toSpeak = b.getTag().toString();
		System.out.println("TALK NOW = " + toSpeak);

		DatabaseHandler db = new DatabaseHandler(getApplicationContext());
		db.insertLabel(toSpeak);

		ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);

		Integer timeSpeak = times.get(toSpeak);
		if (timeSpeak == null) {
			timeSpeak = 0;
			times.put(toSpeak, timeSpeak);
		}

		timeSpeak++;

		times.put(toSpeak, timeSpeak);

		try {
			ArrayList<View> views = getViewsByTag((ViewGroup) (getWindow()
					.getDecorView().getRootView()), toSpeak);
			for (int i = 0; i < views.size(); i++) {

				View myView = views.get(i);

				Button button = (Button) myView;

				button.setText(timeSpeak + "");

			}
		} catch (Exception e) {
			System.out.println("=================================");
			e.printStackTrace();
		}

	}

	static Hashtable<String, Integer> times = null;// new Hashtable<String,
													// Integer>();

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.alphabet, menu);
		return true;
	}

	@Override
	public void onPause() {
		if (ttobj != null) {
			ttobj.stop();
			ttobj.shutdown();
		}
		super.onPause();
	}

	private static ArrayList<View> getViewsByTag(ViewGroup root, String tag) {
		ArrayList<View> views = new ArrayList<View>();
		final int childCount = root.getChildCount();
		for (int i = 0; i < childCount; i++) {
			final View child = root.getChildAt(i);
			if (child instanceof ViewGroup) {
				views.addAll(getViewsByTag((ViewGroup) child, tag));
			}

			final Object tagObj = child.getTag();
			if (tagObj != null && tagObj.equals(tag)) {
				views.add(child);
			}

		}
		return views;
	}

}
